package com.ist.viewpagerhelper.mode;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ist.viewpagerhelper.R;
import com.ist.viewpagerhelper.ViewPagerHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/24.
 */

public class BottomIndicator {
    private static final String TAG = "GlideMode";

    private static final int ANIM_IN = 0X101;
    private static final int ANIM_OUT = 0X102;
    private static int mLastPosition = -1;
    public static void showGlideMode(final ViewPagerHelper.Builder builder) {
        // imageviews 用来保存底部 圆点，用于切换状态
        final List<ImageView> imageViews = new ArrayList<>();
        final LinearLayout bottomLayout = (LinearLayout) builder.linearLayout;

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(20, 0, 0, 0); //每个点的间距

        if (builder.usecircle ){
            builder.selector =R.drawable.glide_bottom_selector;
        }

        for (int i = 0; i < builder.views.size(); i++) {
            ImageView imageView = new ImageView(builder.context);
            imageView.setImageResource(builder.selector);
            imageView.setSelected(true);
            targetViewAnim(imageView,ANIM_IN);
            imageView.setLayoutParams(params);
            if (bottomLayout != null) {
                bottomLayout.addView(imageView);
            }
            imageViews.add(imageView);
        }
        // glide mode 下的viewpager 翻页事件
        final View startBtn = builder.startView;
        builder.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d(TAG, "zsr --> onPageScrolled: "+position);
                // 最后一页
                if (position == imageViews.size() - 1) {
                    if (startBtn != null) {
                        startBtn.setVisibility(View.VISIBLE);
                        if (bottomLayout != null) {
                            bottomLayout.setVisibility(View.GONE);
                        }
                        ObjectAnimator animator = ObjectAnimator.ofFloat(startBtn,
                                "alpha", 0, 1);
                        animator.setDuration(500);
                        animator.setInterpolator(new AccelerateInterpolator());
                        animator.start();
                    }
                } else {
                    if (startBtn != null) {
                        startBtn.setVisibility(View.GONE);
                        if (bottomLayout != null) {
                            bottomLayout.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {

                Log.d(TAG, "zsr --> onPageSelected: "+position);

                View lastView ;
                if (mLastPosition >= 0){
                    lastView = imageViews.get(mLastPosition);
                    if (lastView != null){
                        //lastView.setBackgroundResource(R.drawable.glide_bottom_circle_normal);
                        targetViewAnim(lastView,ANIM_IN);
                    }
                }


                View view = imageViews.get(position);
                if (view != null){
                    view.setBackgroundResource(R.drawable.glide_bottom_circle);
                    targetViewAnim(view,ANIM_OUT);
                }

                mLastPosition = position;

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public static void targetViewAnim(View view,int type){
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator scaleX = null;
        ObjectAnimator scaleY = null;
        ObjectAnimator alpha = null;
        if (type == ANIM_OUT){
            scaleX = ObjectAnimator.ofFloat(view,"scaleX",1.0f,1.7f);
            scaleY = ObjectAnimator.ofFloat(view,"scaleY",1.0f,1.7f);
            alpha = ObjectAnimator.ofFloat(view,"alpha",0.5f,1.0f);
        }else{
            scaleX = ObjectAnimator.ofFloat(view,"scaleX",1.7f,1.0f);
            scaleY = ObjectAnimator.ofFloat(view,"scaleY",1.7f,1.0f);
            alpha = ObjectAnimator.ofFloat(view,"alpha",1.0f,0.5f);
        }
        set.play(scaleX).with(scaleY).with(alpha);
        set.setDuration(300);
        set.setInterpolator(new AccelerateDecelerateInterpolator());
        set.start();
    }
}
